﻿using Models.WebChat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business.Uin
{
    /// <summary>
    /// 用户和分组关系
    /// </summary>
    public class UinGroupInfoManager
    {
        private static List<UinGroupInfo> _UinGroupInfoList = null;
        private static int _Index = 1;
        private static object _Lock = new object();
        static UinGroupInfoManager()
        {
            _UinGroupInfoList = new List<UinGroupInfo> {
                new UinGroupInfo{ Id = _Index++, Uin_groupid=100004, Uin_userid=100000}
            };
        }
        /// <summary>
        /// 添加用户与群组关系
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="groupid"></param>
        /// <returns></returns>
        public bool AddUinGroupInfo(int userid, int groupid)
        {
            if (_UinGroupInfoList.Where(u => u.Uin_userid == userid && u.Uin_groupid == groupid).FirstOrDefault() == null)
            {
                lock (_Lock)
                {
                    if (_UinGroupInfoList.Where(u => u.Uin_userid == userid && u.Uin_groupid == groupid).FirstOrDefault() == null)
                    {
                        _UinGroupInfoList.Add(new UinGroupInfo { Id = _Index++, Uin_groupid = groupid, Uin_userid = userid });
                        return true;
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 移除关系
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="groupid"></param>
        /// <returns></returns>
        public bool RemoveUinGroupInfo(int userid, int groupid)
        {
            UinGroupInfo info = null;
            if ((info = _UinGroupInfoList.Where(u => u.Uin_userid == userid && u.Uin_groupid == groupid).FirstOrDefault()) != null)
            {
                _UinGroupInfoList.Remove(info);
                return true;
            }
            return false;
        }
        /// <summary>
        /// 根据组id获取组成员id
        /// </summary>
        /// <param name="groupid"></param>
        /// <returns></returns>
        public List<int> GetUinuseridList(int groupid)
        {
            return _UinGroupInfoList.Where(u => u.Uin_groupid == groupid).Select(u=>u.Uin_userid).ToList();
        }
        /// <summary>
        /// 根据用户id获取所有该用户所在的群id
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public List<int> GetUingroupidList(int userid)
        {
            return _UinGroupInfoList.Where(u => u.Uin_userid == userid).Select(u => u.Uin_groupid).ToList();
        }
    }
}
